package com.itheima.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmployeeDao;
import com.itheima.pojo.Employee;
import com.itheima.result.R;
import com.itheima.service.EmployeeService;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpSession;
import java.sql.SQLIntegrityConstraintViolationException;
import java.time.LocalDateTime;

/**
 * @Author: 斌
 * @DateTime: 2023年02月05日  20:39:09
 * @Description: com.itheima.service.Impl
 * @Version: 1.0
 */
@Service
@Slf4j
public class EmployeeServiceImpl extends ServiceImpl<EmployeeDao, Employee> implements EmployeeService {
    @Autowired
    private EmployeeDao employeeDao;

    /**
     * 登录
     * @param employee
     * @param session
     * @return
     */
    @Override
    public R login(Employee employee, HttpSession session) {

        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeDao.selectOne(queryWrapper);
        if (emp != null){
            if (emp.getStatus() == 0){
               return R.error("账号已禁用");
            }
            String pwd = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
            log.info("加密后的密码是：" + pwd);
            if (emp.getPassword().equals(pwd) ){
                log.info("添加的id:{}",emp.getId());
                session.setAttribute("employee",emp.getId());
               return R.success(emp);
            }

        }
        return R.error("用户名或密码错误");
    }

    /**
     * 退出
     * @param session
     * @return
     */
    @Override
    public R<String> logout(HttpSession session) {

            session.removeAttribute("employee");
            return R.success("退出成功");



    }

    /**
     * 分页查询员工数据
     * @param page
     * @param pageSize
     * @param employee
     * @return
     */

    @Override
    public R tabs(Integer page,Integer pageSize,Employee employee) {
        Page<Employee> empPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(employee.getName()),Employee::getName,employee.getName());
        Page<Employee> employeePage = employeeDao.selectPage(empPage, wrapper);
        return R.success(employeePage);
    }

    /**
     * 新增员工
     * 全局异常处理器会自动捕获异常，不需要try{}catch(){}
     * @param employee
     * @param session
     * @return
     */

    @Override
    public R addEmp(Employee employee,HttpSession session) {

            //从前端传回的数据需要进行二次设置，密码默认123456，md5加密，createTime。updateTime，createUser，updateUser
            if (employee != null) {
                String pwd = DigestUtils.md5DigestAsHex("123456".getBytes());
                LocalDateTime now = LocalDateTime.now();
                employee.setPassword(pwd);
                employee.setCreateTime(now);
                employee.setUpdateTime(now);
                employee.setCreateUser((Long) session.getAttribute("employee"));
                employee.setUpdateUser((Long) session.getAttribute("employee"));
                int result = employeeDao.insert(employee);
                return result > 0 ? R.success("添加成功") : R.error("添加失败,请重试");
            }
        return R.error("数据异常！！！");
    }

    @Override
    public R queryEmpById(Long id) {
        if (id != null){
            Employee employee = employeeDao.selectById(id);
            return R.success(employee);
        }
        return R.error("数据更新失败");
    }

    /**
     * 员工的禁用和启用
     * @param employee
     * @return
     */

    @Override
    public R updateStatus(Employee employee,HttpSession session) {
        if (employee != null) {
            //修改用户要设置更新时间和更新人
            LocalDateTime now = LocalDateTime.now();
            employee.setUpdateTime(now);
            employee.setUpdateUser((Long) session.getAttribute("employee"));
            int result = employeeDao.updateById(employee);
            return result > 0 ? R.success("修改成功") : R.error("修改失败");
        }
        return R.error("数据异常!!!");
    }
}
